<?php

namespace App\Models;


use Illuminate\Database\Eloquent\SoftDeletes;

class LuckyBag extends BaseModels
{
    use SoftDeletes;

    protected $table = 'lucky_bags';
    protected $primaryKey = 'id';

    const SHARE_CODE_PREFIX = 'LB';

    const TOTAL_BAG_NUM = 8;

    const STATUS_OK = 1;
    const STATUS_DONE = 2;//福袋已被抢完
    const STATUS_GRABBED = 3;//已抢福袋
    const STATUS_EXPIRED = 4;//福袋过期

    const EXPIRE_TIME = 60 *  86400; //2个月
    const EXPIRE_TIME_GRABBED = 2 * 86400; //2天

    public function logs()
    {
        return $this->hasMany(LuckyBagLog::class, 'bag_id');
    }

    public function getId()
    {
        return $this->id;
    }

    public function getStudentId()
    {
        return $this->student_id;
    }

    public function setStudentId($value)
    {
        $this->student_id = $value;
    }

    public function getAppointmentId()
    {
        return $this->appointment_id;
    }

    public function setAppointmentId($value)
    {
        $this->appointment_id = $value;
    }

    public function getShareCode()
    {
        return $this->share_code;
    }

    public function setShareCode($value)
    {
        $this->share_code = $value;
    }

    public function getGrabNum()
    {
        return $this->grab_num;
    }

    public function increaseGrabNum($value = 1)
    {
        $this->grab_num += $value;
    }

    public function getCreatedAt()
    {
        return $this->created_at;
    }

    public function setCreatedAt($value)
    {
        $this->created_at = $value;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($value)
    {
        $this->status = $value;
    }

    public function checkLuckyBagIsExpired()
    {
        $createTimeStamp = $this->getCreatedAt()->getTimestamp();
        if ($createTimeStamp + self::EXPIRE_TIME < time()) {
            return true;
        }

        $firstGrab = LuckyBagLog::where('bag_id', $this->getId())->first();
        if (!empty($firstGrab)) {
            if ($firstGrab->getCreatedAt()->getTimestamp() + self::EXPIRE_TIME_GRABBED < time()) {
                return true;
            }
        }

        return false;
    }
}